package com.apes.auto.maintain.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.auto.maintain.dao.MaintainPeriodAppDao;
import com.apes.auto.maintain.model.MaintainPeriodApp;
import com.apes.auto.maintain.repository.MaintainPeriodAppRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName MaintainPeriodAppService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/8/9 9:16
 * @Version 1.0
 **/
@Service("maintainPeriodAppService")
public class MaintainPeriodAppService extends DomainService {
    @Autowired
    private MaintainPeriodAppRepository maintainPeriodAppRepository;
    @Autowired
    private MaintainPeriodAppDao maintainPeriodAppDao;

    public MaintainPeriodApp save(SimpleRequest request) {
        MaintainPeriodApp o = request.getO(MaintainPeriodApp.class);
        o.setTS01(DateUtil.format(new Date(), "yyyyMMdd.HHmmss.SSS"));
        MaintainPeriodApp maintainPeriodApp = maintainPeriodAppRepository.saveAndFlush(o);
        return maintainPeriodApp;
    }

    public String delete(SimpleRequest request){
        MaintainPeriodApp o = request.getO(MaintainPeriodApp.class);
        maintainPeriodAppRepository.delete(o);
        return "删除成功";
    }

    /**
     * 车辆基础信息(保养信息)
     * @param request
     * @return
     */
    public JSONObject findByCXID(SimpleRequest request){
        JSONObject param = request.getJO();
        JSONObject maintainPeriodApp = new JSONObject();
        JSONObject result = new JSONObject();
        JSONArray maintainPeriodAppList = maintainPeriodAppDao.findByCXID(param);

        if(maintainPeriodAppList !=null && maintainPeriodAppList.size()>0){
            maintainPeriodApp = maintainPeriodAppList.getJSONObject(0);
        }
        if (maintainPeriodApp!=null&&maintainPeriodApp.getString("PZCXID")!=null&& maintainPeriodApp.get("JCYLX") != null && maintainPeriodApp.get("JYGG") != null){
            result.put("JCYLXArray", maintainPeriodAppDao.getJCYLXCommon(maintainPeriodApp));
        } else {
            result.put("JCYLXArray", new JSONArray());
        }
        return result;
    }
}
